﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports BusinessRule.IBusiness
Imports DatabaseAccess.IDAL
Imports System.Data
Imports BusinessRule.DTO
Imports BusinessRule.Common
Imports DatabaseAccess.DALFactory
Imports External.APICalling
Imports Camstar.WebClient.WebServicesProxy.InSiteWebServices

Public Class uMESResourceParameterBusiness

    Implements uMESResourceParameterIBusiness

    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()

#Region "按ResourceType和关键字查询Resource"
    Function GetResourceList(ByVal para As Dictionary(Of String, String)) As uMESProcessResult Implements uMESResourceParameterIBusiness.GetResourceList
        Dim strQuery As String = "SELECT r.resourceename,r.description,r.resourcenum,r.resourcename" & vbCrLf & _
                                "FROM resourcedef r" & vbCrLf & _
                                "LEFT JOIN resourcetype rt ON rt.resourcetypeid = r.resourcetypeid" & vbCrLf & _
                                "WHERE 1 = 1" & vbCrLf

        If para.ContainsKey("ResourceType") = True Then
            strQuery += "AND rt.resourcetypename = '" & para("ResourceType").ToString & "'" & vbCrLf
        End If

        If para.ContainsKey("KeyStr") = True Then
            strQuery += "AND (LOWER(r.resourceename) LIKE '%" & para("KeyStr").ToLower & "%'" & vbCrLf & _
                        "OR LOWER(r.description) LIKE '%" & para("KeyStr").ToLower & "%'" & vbCrLf & _
                        "OR LOWER(r.resourcenum) LIKE '%" & para("KeyStr").ToLower & "%'" & vbCrLf & _
                        "OR LOWER(r.resourcename) LIKE '%" & para("KeyStr").ToLower & "%')" & vbCrLf
        End If

        strQuery += "ORDER BY r.resourceename ASC" & vbCrLf

        Dim result As New uMESProcessResult

        Try
            Dim DT As DataTable = dal.GetMainViewInfo(strQuery)
            result.Result = True
            result.DBTable = DT
        Catch ex As Exception
            result.Result = False
            result.Message = ex.Message
        End Try

        Return result
    End Function
#End Region

#Region "按ResourceName查询ResourceParameter"
    Function GetResourceParameterList(ByVal para As Dictionary(Of String, String)) As uMESProcessResult Implements uMESResourceParameterIBusiness.GetResourceParameterList
        Dim strQuery As String = "SELECT p.parametername,p.paramaxvalue as maxvalue,p.paraminvalue as minvalue,p.pararealvalue as realvalue," & vbCrLf & _
                                "   p.notes,r.resourcename" & vbCrLf & _
                                "FROM parameters p" & vbCrLf & _
                                "LEFT JOIN resourcedefparameters rp ON rp.parametersid = p.parametersid" & vbCrLf & _
                                "LEFT JOIN resourcedef r ON r.resourceid = rp.resourceid" & vbCrLf & _
                                "WHERE 1 = 1" & vbCrLf

        If para.ContainsKey("ResourceName") = True Then
            strQuery += "AND r.resourcename = '" & para("ResourceName").ToString & "'" & vbCrLf
        End If

        strQuery += "ORDER BY p.parametername ASC" & vbCrLf

        Dim result As New uMESProcessResult

        Try
            Dim DT As DataTable = dal.GetMainViewInfo(strQuery)
            result.Result = True
            result.DBTable = DT
        Catch ex As Exception
            result.Result = False
            result.Message = ex.Message
        End Try

        Return result
    End Function
#End Region

#Region "按ResourceType查询ResourceParameter"
    Function GetResourceTypeParameterList(ByVal para As Dictionary(Of String, String)) As uMESProcessResult Implements uMESResourceParameterIBusiness.GetResourceTypeParameterList
        Dim strQuery As String = "SELECT rp.resourceparametername AS parametername,0 AS realvalue,0 AS maxvalue,0 AS minvalue,'' AS notes" & vbCrLf & _
                                "FROM resourcetyperesourceparameter rtp" & vbCrLf & _
                                "LEFT JOIN resourceparameter rp ON rp.resourceparameterid = rtp.resourceparameterid" & vbCrLf & _
                                "LEFT JOIN resourcetype rt ON rt.resourcetypeid = rtp.resourcetypeid" & vbCrLf & _
                                "WHERE 1 = 1" & vbCrLf

        If para.ContainsKey("ResourceName") = True Then
            strQuery += "AND rt.resourcetypename = (" & vbCrLf & _
                        "   SELECT rt1.resourcetypename" & vbCrLf & _
                        "   FROM resourcetype rt1" & vbCrLf & _
                        "   LEFT JOIN resourcedef r ON r.resourcetypeid = rt1.resourcetypeid" & vbCrLf & _
                        "   WHERE r.resourcename = '" & para("ResourceName").ToString & "'" & vbCrLf & _
                        ")" & vbCrLf
        End If

        strQuery += "ORDER BY rp.resourceparametername ASC" & vbCrLf

        Dim result As New uMESProcessResult

        Try
            Dim DT As DataTable = dal.GetMainViewInfo(strQuery)
            result.Result = True
            result.DBTable = DT
        Catch ex As Exception
            result.Result = False
            result.Message = ex.Message
        End Try

        Return result
    End Function
#End Region
End Class
